#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
		sort(intervals.begin(), intervals.end(), [](vector<int> & a, vector<int> & b){ // 注意这里必须要是&,否则排序会很耗时
			if(a[0] == b[0]){
				return a[1] < b[1];
			}
			return a[0] < b[0];
		});
		int left = intervals[0][0];
		int right = intervals[0][1];
		vector<vector<int>> res;
		for(auto &t : intervals){
			if(right >= t[0]){
				if(right <= t[1])
					right = t[1];
			}else{
				res.push_back({left, right});
				left = t[0];
				right = t[1];
			}
		}
		res.push_back({left, right});
		return res;
    }
};

int main(){
	Solution so;
	vector<vector<int>> intervals = {{1,3},{1,4},{2,6},{8,10},{15,18}};
	
	
	auto res = so.merge(intervals);
	for(auto items : res){
		for(auto item : items){
			cout << item << ",";
		}
		cout << endl;
	}
	return 0;
}